/*
 * Profiles.java
 *
 * Created on 30 wrzesień 2007, 17:19
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package info.mikrotubula.db;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 * Entity class Profiles
 * 
 * @author Krystian
 */
@Entity
@Table(name="PROFILES")
@NamedQueries( {
        @NamedQuery(name = "Profiles.findByPrfId", query = "SELECT p FROM Profiles p WHERE p.prfId = :prfId"),
        @NamedQuery(name = "Profiles.findByPrfName", query = "SELECT p FROM Profiles p WHERE p.prfName = :prfName"),
        @NamedQuery(name = "Profiles.findAll", query = "SELECT p FROM Profiles p")
    })
public class Profiles implements Serializable {

    @Id
    @Column(name = "PRF_ID", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer prfId;
    
    @Column(name = "PRF_NAME")
    private String prfName;
    
    @ManyToMany
    @JoinTable(
        name="PROFILE_RIGHTS",
        joinColumns=
            @JoinColumn(name="PR_PRF_ID", referencedColumnName="PRF_ID"),
        inverseJoinColumns=
            @JoinColumn(name="PR_RGH_ID", referencedColumnName="RGH_ID")
    )
    private Collection<Rights> rights;
    
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "profile")
    private Collection<Users> users;    
        
        
    /** Creates a new instance of Profiles */
    public Profiles() {
    }

    /**
     * Gets the id of this Profiles.
     * @return the id
     */
    public Integer getPrfId() {
        return this.prfId;
    }

    /**
     * Sets the id of this Profiles to the specified value.
     * @param id the new id
     */
    public void setPrfId(Integer prfId) {
        this.prfId = prfId;
    }

    /**
     * Returns a hash code value for the object.  This implementation computes 
     * a hash code value based on the id fields in this object.
     * @return a hash code value for this object.
     */
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (this.prfId != null ? this.prfId.hashCode() : 0);
        return hash;
    }

    /**
     * Determines whether another object is equal to this Profiles.  The result is 
     * <code>true</code> if and only if the argument is not null and is a Profiles object that 
     * has the same id field values as this object.
     * @param object the reference object with which to compare
     * @return <code>true</code> if this object is the same as the argument;
     * <code>false</code> otherwise.
     */
    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Profiles)) {
            return false;
        }
        Profiles other = (Profiles)object;
        if (this.prfId != other.prfId && (this.prfId == null || !this.prfId.equals(other.prfId))) return false;
        return true;
    }

    /**
     * Returns a string representation of the object.  This implementation constructs 
     * that representation based on the id fields.
     * @return a string representation of the object.
     */
    @Override
    public String toString() {
        return prfName;
    }

    public String getPrfName() {
        return prfName;
    }

    public void setPrfName(String prfName) {
        this.prfName = prfName;
    }

    public Collection<Rights> getRights() {
        return rights;
    }

    public void setRights(Collection<Rights> rights) {
        this.rights = rights;
    }

    public void addRight(Rights right) {
        this.getRights().add(right);
    }
    
    public void dropRight(Rights right) {
        this.getRights().remove(right);
    }

    public Collection<Users> getUsers() {
        return users;
    }

    public void setUsers(Collection<Users> users) {
        this.users = users;
    }
    
}
